﻿using Rug.Osc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DmxTestTool.Comm
{
    internal class OscSend : DmxLib.IDmxSend
    {
        private bool isStart = false;
        private int frame;
        private byte[] sendData;
        private System.Timers.Timer timer;
        /// <summary>
        /// 是否启动
        /// </summary>
        public bool IsStart
        {
            private set
            {
                isStart = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("IsStart"));
            }
            get => isStart;
        }
        private  OscSender send;
        /// <summary>
        /// artnet 发送类  按帧率发循环发送
        /// </summary>
        /// <param name="OscIPEndPoint">OscIPEndPoint 地址</param>
        /// <param name="universe">artnet域</param>
        /// <param name="frame">帧率 默认30</param>
        public OscSend(string oscIPEndPoint, int frame = 30)
        {
           
            sendData = new byte[512];
            var ipendPoint = VCLib.Converts.IPConverts.ConverToIPEndPoint(oscIPEndPoint);
            send = new OscSender(ipendPoint.Address, ipendPoint.Port);

            this.frame = frame;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {


            //for (int i = 0; i < UniverseCount; i++)

            //    sendData.Add(new byte[512]);
            try
            {
                send.Send(new OscMessage("/dmx",sendData));
            }
            catch (Exception)
            {


            }

            // }

        }
        public void Dispose()
        {
            Stop();
            send.Dispose();
        }

        public void Start()
        {
            if (IsStart)
                return;
            //Stop();
            send.Connect();
               timer = new System.Timers.Timer();
            timer.Elapsed += Timer_Elapsed;
            timer.Interval = 1000 / frame;
            timer.Start();
            IsStart = true;
        }

        public void Stop()
        {
            if (!IsStart)
                return;
            if (timer != null)
            {
                timer.Stop();
                timer.Dispose();
                timer = null;

            }
            send.Close();
            IsStart = false;
        }

        public void UpdateData(byte[] dmxval)
        {
            if (dmxval == null)
                throw new ArgumentNullException("dmxval is null");
            for (int i = 0; i < dmxval.Length && i < 512; i++)
            {
                sendData[i] = dmxval[i];
            }
        }

        public void UpdateData(int index, byte value)
        {
            if (index >= 0 & index < 512)
                sendData[index] = value;
        }

        public void UpdateData(int startIndex, byte[] dmxval)
        {
            if (dmxval == null)
                throw new ArgumentNullException("dmxval is null");
            if (startIndex >= 0 & startIndex < 512)
            {
                for (int i = startIndex, j = 0; j < dmxval.Length && i < 512; i++, j++)
                {
                    sendData[i] = dmxval[j];
                }
            }
        }

        public void UpdateData(byte[] dmxval, int startIndex, int count)
        {
            if (dmxval == null)
                throw new ArgumentNullException("dmxval is null");
            if (dmxval.Length < count)
                throw new ArgumentException("dmxval 数组长度比count小");
            if (startIndex >= 0 & startIndex < 512)
            {
                for (int i = startIndex, j = 0; i < dmxval.Length && i < 512 && j < count; i++, j++)
                {
                    sendData[i] = dmxval[i];
                }
            }
        }
    }
}
